package controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import model.Coach;
import model.Member;
import model.NegativeTransaction;
import model.PositiveTransaction;
import model.Room;
import model.Subscription;
import database.DatabaseManager;


public class TUI{
	
	private Scanner			scanner;
	private FitnessClub		fitnessClub;
	private DatabaseManager	dbManager;
	
	
	public TUI( FitnessClub fitnessClub ) {
		this.fitnessClub = fitnessClub;
		dbManager = DatabaseManager.getInstance();
		scanner = new Scanner( System.in );
		GUI.start( dbManager.getTransactionDao() );
		dbManager.addTransaction( new PositiveTransaction( 200, "description" ) );
		showMainMenu();
	}
	
	
	// Main menu depth:0
	public void showMainMenu() {
		
		System.out.println( "[1] Administratie" );
		System.out.println( "[2] Financieel" );
		System.out.println( "[3] Leden interactie" );
		System.out.println( "[4] Systeem resetten" );
		System.out.println( "[0] Sluiten" );
		
		switch( readInput( 4 ) ){
			case 1:
				showAdministrationMenu();
				break;
			case 2:
				showFinancialMenu();
				break;
			case 3:
				showMemberInteractionMenu();
				break;
			case 4:
				showBackups();
				break;
			default:
				System.exit( 0 );
				break;
		}
	}
	
	
	// Admin menu depth:1
	public void showAdministrationMenu() {
		System.out.println( fitnessClub.getName() );
		System.out.println( "[1] Toon leden per ruimte" );
		System.out.println( "[2] Lid opties" );
		System.out.println( "[3] Ruimte opties" );
		System.out.println( "[4] Abonnement opties" );
		System.out.println( "[5] Coach opties" );
		System.out.println( "[0] terug" );
		
		switch( readInput( 5 ) ){
			case 1:
				showMembersForEachRoom();
				break;
			case 2:
				showOptionsMenu( "Lid" );
				break;
			case 3:
				showOptionsMenu( "Ruimte" );
				break;
			case 4:
				showOptionsMenu( "Abonnement" );
				break;
			case 5:
				showOptionsMenu( "Coach" );
				break;
			default:
				showMainMenu();
				break;
		}
	}
	
	
	// depth:2
	public void showMembersForEachRoom() {
		for( Room room : dbManager.getRooms() ){
			System.out.println( room.getName() + " " + room.getVisitors().size() );
		}
		
		System.out.println( "[0] terug" );
		if( readInput( 0 ) == 0 ){
			showAdministrationMenu();
		}
	}
	
	
	// depth:2
	public void showOptionsMenu( String value ) {
		System.out.println( "[1] " + value + " toevoegen" );
		System.out.println( "[2] " + value + " verwijderen" );
		System.out.println( "[3] " + value + " gegevens aanpassen" );
		System.out.println( "[0] terug" );
		int input = readInput( 3 );
		switch( input ){
			case 1:
				if( value.equals( "Lid" ) ){
					addMemberMenu( value );
				} else if( value.equals( "Ruimte" ) ){
					// TODO
//					addRoomMenu( value );
				} else if( value.equals( "Abonnement" ) ){
					addSubscriptionMenu( value );
				} else if( value.equals( "Coach" ) ){
					//TODO
//					addCoachMenu( value );
				}
				break;
			case 2:
				deleteMenu( value );
				break;
			case 3:
				// TODO
				changeDetailsMenu( value );
				break;
			default:
				showMainMenu();
				break;
		}
	}
	
	
	// depth:3
	public void addMemberMenu( String value ) {
		System.out.println( "Vul hier de naam in:" );
		System.out.println( "[0] terug" );
		String name = scanner.next();
		if( name.equals( "0" ) ){
			showAdministrationMenu();
		}
		System.out.println( "Kies het abonnement:" );
		List<Subscription> subs = dbManager.getSubscriptions();
		for( int i = 0; i < subs.size(); i++ ){
			System.out.println( "[" + ( i + 1 ) + "] " + subs.get( i ).getName() );
		}
		System.out.println( "[0] terug" );
		int input = readInput( subs.size() );
		
		if( input == 0 ){
			showOptionsMenu( value );
		} else{
			dbManager.addMember( name, subs.get( input - 1 ) );
		}
	}
	
	public void addSubscriptionMenu(String value){
		System.out.println( "Type de naam, prijs en lijst van getallen uit onderstaande ruimtes gescheiden door spaties:" );
		
		List<Room> rooms = dbManager.getRooms();
		for( int i = 0; i < rooms.size(); i++ ){
			System.out.println( "[" + ( i + 1 ) + "] " + rooms.get( i ).getName() );
		}
		System.out.println( "[0] terug" );
		
		String input = scanner.next();
		if( input.equals( "0" ) ){
			showOptionsMenu(value);
		}else{
			String[] inputList = input.split( " " );
			List<Room> roomsSubset = new ArrayList<Room>();
			for(int i = 2; i < inputList.length; i++){
				roomsSubset.add( rooms.get( i ) );
			}
			dbManager.addSubscription( new Subscription(inputList[0], roomsSubset, Double.parseDouble( inputList[1]) ) );
		}
	}
	
	// depth:3
	public void deleteMenu( String value ) {
		
		System.out.println( "Selecteer " + value + " die je wilt verwijderen:" );
		if( value.equals( "Lid" ) ){
			List<Member> list = dbManager.getMembers();
			
			for( int i = 0; i < list.size(); i++ ){
				System.out.println( "[" + ( i + 1 ) + "] " + list.get( i ).getName() );
			}
			
			System.out.println( "[0] terug" );
			int input = readInput( list.size() );
			
			if( input == 0 ){
				showOptionsMenu( value );
			} else{
				dbManager.deleteMember( list.get( input - 1 ) );
			}
		} else if( value.equals( "Ruimte" ) ){
			List<Room> list = dbManager.getRooms();
			
			for( int i = 0; i < list.size(); i++ ){
				System.out.println( "[" + ( i + 1 ) + "] " + list.get( i ).getName() );
			}
			
			System.out.println( "[0] terug" );
			int input = readInput( list.size() );
			
			if( input == 0 ){
				showOptionsMenu( value );
			} else{
				dbManager.deleteRoom( list.get( input - 1 ) );
			}
		} else if( value.equals( "Abonnement" ) ){
			List<Subscription> list = dbManager.getSubscriptions();
			
			for( int i = 0; i < list.size(); i++ ){
				System.out.println( "[" + ( i + 1 ) + "] " + list.get( i ).getName() );
			}
			
			System.out.println( "[0] terug" );
			int input = readInput( list.size() );
			
			if( input == 0 ){
				showOptionsMenu( value );
			} else{
				dbManager.deleteSubscription( list.get( input - 1 ) );
			}
		} else if( value.equals( "Coach" ) ){
			List<Coach> list = dbManager.getCoaches();
			
			for( int i = 0; i < list.size(); i++ ){
				System.out.println( "[" + ( i + 1 ) + "] " + list.get( i ).getName() );
			}
			
			System.out.println( "[0] terug" );
			int input = readInput( list.size() );
			
			if( input == 0 ){
				showOptionsMenu( value );
			} else{
				dbManager.deleteCoach( list.get( input - 1 ) );
			}
		}
	}
	
	
	// depth:3
	public void changeDetailsMenu( String value ) {
		System.out.println( "[0] terug" );
		int input = readInput( 0 );
		if( input == 0 ){
			showOptionsMenu( value );
		}
		
	}
	
	
	// Financial menu depth:1
	public void showFinancialMenu() {
		System.out.println( "[1] Winst grafisch weergeven" );
		System.out.println( "[2] Leden grafisch weergeven" );
		System.out.println( "[3] Nieuwe transactie aanmaken" );
		System.out.println( "[0] terug" );
		
		switch( readInput( 3 ) ){
			case 1:
				// TODO voor kevin
				showProfitGraphicalmenu();
				break;
			case 2:
				// TODO voor kevin
				showMembersGraphical();
				break;
			case 3:
				showNewTransactionMenu();
				break;
			default:
				showMainMenu();
				break;
		}
	}
	
	
	// depth:2
	private void showProfitGraphicalmenu() {

		
	}
	
	
	// depth:2
	private void showMembersGraphical() {

		
	}
	
	
	// depth:2
	public void showNewTransactionMenu() {
		System.out
				.println( "type het bedrag en omschrijving gevolgd door pos(inkomst) of neg(uitgave) om aan te geven om wat voor transactie het gaat( alles gescheiden door spaties)" );
		System.out.println( "[0] terug" );
		
		String input = scanner.next();
		
		if( input.equals( "0" ) ){
			showFinancialMenu();
		} else{
			String[] inputSplitUp = input.split( " " );
			if( inputSplitUp[2].equals( "pos" ) ){
				dbManager.addTransaction( new PositiveTransaction( Double.parseDouble( inputSplitUp[0] ), inputSplitUp[1] ) );
			} else if( inputSplitUp[2].equals( "neg" ) ){
				dbManager.addTransaction( new NegativeTransaction( Double.parseDouble( inputSplitUp[0] ), inputSplitUp[1] ) );
			}
		}
	}
	
	
	// Member interaction menu depth:1
	public void showMemberInteractionMenu() {
		System.out.println( "[1] Bedien 'apparaat' in lobby" );
		System.out.println( "[2] Ruimte binnentreden" );
		System.out.println( "[0] terug" );
		// TODO 
		switch( readInput( 2 ) ){
			case 1:
				// showMemberDeviceMenu();
				break;
			case 2:
				// showRoomsToEnterMenu();
				break;
			default:
				showMainMenu();
				break;
		}
	}
	
	
	// System backups depth:1
	public void showBackups() {
		//TODO
		// loop door memento's/backups en toon deze
		List<DatabaseManager.Memento> mementos = fitnessClub.getMementos();
		for( int i = 0; i < mementos.size(); i++ ){
			System.out.println( "[" + ( i + 1 ) + "] " + mementos.get( i ).getDate().toString() );
		}
		System.out.println( "[0] terug" );
		if( readInput( 0 ) == 0 ){
			showAdministrationMenu();
		}
	}
	
	
	public int readInput( int max ) {
		int selection = 0;
		while( true ){
			try{
				selection = Integer.parseInt( scanner.next() );
				if( selection > max || selection < 0 ){
					throw new NumberFormatException();
				}
				return selection;
				
			} catch( NumberFormatException e ){
				System.out.println( "incorrecte input probeer opnieuw" );
			}
		}
	}
}
